﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CM.BL.BE;
using System.Collections.Generic;
using CM.BL.BC;
using Newtonsoft.Json;

namespace WebAppHTML5
{
    public partial class List : System.Web.UI.Page
    {
        private RfcConfigParametersBE _objConn;
        string pRetval = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.HttpMethod.Equals("POST"))
            {
                
            }
            else
            {
                pRetval = "{\"success\":{0}, \"errors\":{\"reason\": \"{1}\"},  ";
                pRetval += "\"lstItemsResale\":{2} ";
                pRetval += "}";
                getItemsResale();
            }

            Response.ContentType = "application/json; charset=UTF-8";
            Response.Write(pRetval);
        }

        private void getItemsResale()
        {
            string retval = string.Empty, message = string.Empty;
            //this.checkLogin();
            
            List<PurchaseOrderBE> lstPO = new List<PurchaseOrderBE>();
            CredencialesController.cargarInfo(Request.Params); //(SIEMPRE) @@@
            string SesID = "";
            if (string.IsNullOrEmpty(Request.Params["Id"]))
            {
                SesID = Session.SessionID;
            }
            else
            {
                SesID = Request.Params["Id"];
            }
            //************************************************************* @@@
            retval = PurchaseOrderBC.getInstance(SesID).getItemsForRelease(out lstPO);
            if (lstPO != null || lstPO.Count > 0)
            {
                string Json = JsonConvert.SerializeObject(lstPO);
                pRetval = pRetval.Replace("{0}", "true");
                pRetval = pRetval.Replace("{1}", retval);
                pRetval = pRetval.Replace("{2}", Json);
            }
            else
            {
                pRetval = pRetval.Replace("{0}", "false");
                pRetval = pRetval.Replace("{1}", retval);
                pRetval = pRetval.Replace("{2}", "[]");
            }
        }
        
        
    }
}
